package mysgs.cards;

import java.util.Vector;

import org.openslash.util.Utilities;

public class SGSCardSuit {
	
	private Vector<Short> gameCards = null;
	private Vector<Short> discardedGameCards = null;
	private Vector<Byte> identities = null;
	private Vector<Short> roles = null;

	public SGSCardSuit(int gameCardCount, byte idTypes[], int roleCount) {
		gameCards = new Vector<Short>(gameCardCount);
		identities = new Vector<Byte>(idTypes.length);
		for( byte identity : idTypes )
		{
			identities.add(identity);
		}
		this.roles = new Vector<Short>(roleCount);
	}
	
	public void makeStructCopyAsDiscardCardHeap() {
		discardedGameCards = new Vector<Short>(gameCards.size());
	}

	/*
	public int GetIdentityCardCount() {
		return identities.size();
	}
	*/
	public void AddRoleCards(short cardIds[]) {
		for(short i=0; i<cardIds.length; i++) {
			this.AddRoleCard(cardIds[i]);
		}
	}
	public void AddRoleCard(short cardId) {
		roles.add(cardId);
	}

	public int GetRoleCardCount() {
		return roles.size();
	}

	private void swapIdentityLoc(int index1, int index2) {
		byte swap = identities.get(index1);
		identities.set(index1, identities.get(index2));
		identities.set(index2, swap);
	}	
	public void swapRoleCardLoc(int index1, int index2) {
		short swap = roles.get(index1);
		roles.set(index1, roles.get(index2));
		roles.set(index2, swap);
	}

	public void ShuffleIdentities(){
		for(int i=0, max = identities.size(); i < 5 * max; i++)
		{
			int index1 = Utilities.getRandomInt(max);
			int index2 = Utilities.getRandomInt(max);
			if(index1 == index2)
			{
				continue;
			}
			swapIdentityLoc(index1, index2);
		}
	}

	public void ShuffleRoles(int previous){
		for(int i=0, max = previous; i < 5 * max; i++)
		{
			int index1 = Utilities.getRandomInt(max);
			int index2 = Utilities.getRandomInt(max);
			if(index1 == index2)
			{
				continue;
			}
			swapRoleCardLoc(index1, index2);
		}
	}

	public void ExpandFullCardHeapOnTop(short cardId) {
		Vector<Short> expanded = new Vector<Short>(gameCards.capacity()+1);
		expanded.add(cardId);
		expanded.addAll(gameCards);
		gameCards = expanded;
	}
	
	public void AddCard(short cardId) {
		gameCards.add(cardId);
	}
	public short getGameCard(int i) {
		return gameCards.get(i);
	}
	public void SetCard(int index, short value) {
		gameCards.set(index, value);
	}
	public int GetCardCount() {
		return gameCards.size();
	}

	public void ShuffleGameCards(){
		shuffle(gameCards);
	}
	private void shuffle(Vector<Short> cards)
	{
		for(int i=0, max = cards.size(); i < 5 * max; i++)
		{
			int index1 = Utilities.getRandomInt(max);
			int index2 = Utilities.getRandomInt(max);
			if(index1 == index2)
			{
				continue;
			}
			short swap = cards.get(index1);
			cards.set(index1, cards.get(index2));
			cards.set(index2, swap);
		}
	}
	public boolean isEmpty()
	{
		return gameCards.isEmpty();
	}
	
	public byte getIndentity(int index)
	{
		return identities.get(index).byteValue();
	}

	public short getRoleCard(int index) {
		return roles.get(index).shortValue();
	}

	public void AddCardIntoDCH(short cardId) {
		discardedGameCards.add(cardId);
	}

	public int getDCHCardCount() {
		return discardedGameCards.size();
	}

	public void shuffleDCHAndMoveToCH() {
		gameCards.clear();
		gameCards.addAll(discardedGameCards);
		discardedGameCards.clear();
		shuffle(gameCards);		
	}

	public void ShuffleDCH() {
		shuffle(discardedGameCards);
	}
	public void MergeIntoCHAndReleaseDCH()
	{
		shuffle(discardedGameCards);
		gameCards.addAll(discardedGameCards);
		discardedGameCards.clear();
	}
}
